<?php

##################################################
#
# Copyright (c) 2004-2006 OIC Group, Inc.
# Written and Designed by James Hunt
#
# This file is part of Exponent
#
# Exponent is free software; you can redistribute
# it and/or modify it under the terms of the GNU
# General Public License as published by the Free
# Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# GPL: http://www.gnu.org/licenses/gpl.txt
#
##################################################

/**
* Database structure
* ALTER TABLE `ag_user_address` ADD `church` TEXT NOT NULL DEFAULT '',
ADD `denomination` TEXT NOT NULL DEFAULT ''
  ALTER TABLE `ag_user_address` CHANGE `address1` `address1` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
CHANGE `address2` `address2` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
CHANGE `city` `city` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
CHANGE `country` `country` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL   
* 
* 
* Change logs
* Jan 30, 2011 - trungdt - Remove State & Zip attrs
*                          Now city -> district
*                              country -> province
*                          Add church -> Church name
*                              denomination -> Denomination name
*                          Change all varchar field type to TEXT
* 
* 
*      
*/

class addressextension {
	function name() { return exponent_lang_loadKey('subsystems/users/profileextensions/addressextension.php','extension_name'); }
	function author() { return "James Hunt, upgraded by trungdt - ABSoft VN"; }
	function description() { return exponent_lang_loadKey('subsystems/users/profileextensions/addressextension.php','extension_description'); }

	function modifyForm($form,$user) { // new if !isset($user->id)
	    global $db;
		$i18n = exponent_lang_loadFile('subsystems/users/profileextensions/addressextension.php');
	
		if (!isset($user->user_address) || $user->user_address == null) {
			$user->user_address = addressextension::_blankAddress();
		}
		$form->register(null,"",new htmlcontrol('<br><div class="textmodule"><div class="textmodule_title"><h3>' . $i18n['header'] . '</h3> </div> </div>'));
		
        $form->register("address1",$i18n['address1'], new textcontrol($user->user_address->address1,55,false,250,"",true));
		$form->register("address2",$i18n['address2'], new textcontrol($user->user_address->address2,55,false,250,"",false));
		
        $country=$db->selectDropdown('billing_country','name');
            //country
            foreach($country as $value=>$caption)
            {

                $country1.='<option value="'.$caption.'"' ;
                if(intval($user->user_address->country)==$caption)  $country1.=' selected'  ;
                $country1.='>'.$caption.'</option>';
            }

            $html_ct='<select class="select valid" id="country" name="country" size="1"  style="width: 180px;">
            '.$country1.'
            </select>' ;  
        
        $html=' <table border="0"> 
        <tbody>
        <tr>
             <td><div id="nameControl" class="text control "><label><span class="label">'.$i18n['city'].'</span></label></div></td>
            <td><div id="nameControl" class="text control "><label><span class="label">'.$i18n['country'].'</span></label></div></td>
        </tr>
        <tr>
           
            <td style="vertical-align: top;">
                <span class="control">
                <input type="text" size="11"  value="'.$user->user_address->city.'" class="control text required" name="address_city" style="width: 180px;">
            </span></td>
            <td style="vertical-align: top;">
                <span class="control">
                   '.$html_ct.'</span>
            </td>
        </tr>
        
        <tr>
             <td><div id="nameControl" class="text control "><label><span class="label">'.$i18n['church'].'</span></label></div></td>
            <td><div id="nameControl" class="text control "><label><span class="label">'.$i18n['denomination'].'</span></label></div></td>
        </tr>
        <tr>
           
            <td style="vertical-align: top;">
                <span class="control">
                <input type="text" size="11"  value="'.$user->user_address->church.'" class="control text required" name="church" style="width: 180px;">
            </span></td>
            <td style="vertical-align: top;">
                <span class="control">
                    <input type="text" size="12"  value="'.$user->user_address->denomination.'" class="text required " name="denomination" style="width: 180px;">
                </span>
            </td>
        </tr>
        
       
        
    </tbody>  </table>
    
      
        ';
        $form->register(null,"",new htmlcontrol($html));
        
        
        //$form->register("city",$i18n['city'], new textcontrol($user->user_address->city,60,false,250,"",true));
        // jan 30, 2011 - trungdt - comment 2 line code return state & zip - begin
		//$form->register("state",$i18n['state'], new textcontrol($user->user_address->state));
		//$form->register("zip",$i18n['zip'], new textcontrol($user->user_address->zip));
        // jan 30, 2011 - trungdt - comment 2 line code return state & zip - end
        /**
        * 
        * @TODO: Jan 30, 2011 - trungdt - I think we can implement this field better by using combobox
        *                                 We must show all provinces  
        */
		//$form->register("country",$i18n['country'], new textcontrol($user->user_address->country,60,false,250,"",true));
        //$form->register("church",$i18n['church'], new textcontrol($user->user_address->church,60,false,250,"",true));
        /**
        * 
        * @TODO: Jan 30, 2011 - trungdt - I think we can implement this field better by using combobox
        */
        //$form->register("denomination",$i18n['denomination'], new textcontrol($user->user_address->denomination,60,false,250,"",true));
		
		return $form;
	}
	
	function saveProfile($values,$user,$is_new) {
		global $db;
		$db->delete("user_address","uid=".$user->id);
		
		$address = null;
		$address->uid = intval( $user->id );
		$address->address1 = $values['address1'];
		$address->address2 = $values['address2'];
		$address->city = $values['address_city'];
        // jan 30, 2011 - trungdt - comment 2 line code return state & zip
		//$address->state = $values['state'];
		//$address->zip = $values['zip'];
        // add church and denomination
        $address->church=$values['church'];
        $address->denomination=$values['denomination'];
        // jan 30, 2011 - trungdt - end
		$address->country = $values['country'];
		
		$db->insertObject($address,"user_address");
		
		$user->user_address = $address;
		unset($user->user_address->uid);
		return $user;
	}
	
	function getProfile($user) {
		global $db;
		if (!isset($user->id)) {
			$user->user_address = addressextension::_blankAddress();
		} else {
			$user->user_address = $db->selectObject("user_address","uid=".$user->id);
		}
		return $user;
	}
	
	function cleanup($user) {
		global $db;
		$db->delete("user_address","uid=".$user->id);
	}
	
	function clear() {
		global $db;
		$db->delete("user_address");
	}
	
	function hasData() {
		global $db;
		return ($db->countObjects("user_address") != 0);
	}
	
	function _blankAddress() {
		$address = null;
		$address->address1 = "";
		$address->address2 = "";
		$address->city = "";
		// Jan 30, 2011 - trungdt - comment 2 line code return state & zip
        //$address->state = "";
		//$address->zip = "";
        // add 2 field church & denomination
        $address->church='';
        $address->denomination='';
        // Jan 30, 2011 - trungdt - end
		$address->country = "";
        
		return $address;
	}
}

?>